package com.geo.geotool.service.impl;

import com.geo.geotool.entity.sql.DFeatureEntity;
import com.geo.geotool.entity.sql.QFeatureEntity;
import com.geo.geotool.entity.sql.SFeatureEntity;
import com.geo.geotool.entity.sql.UFeatureEntity;
import com.geo.geotool.mapper.FeatureMapper;
import com.geo.geotool.service.IFeatureService;
import com.geo.geotool.service.trans.GeometryFormatCapture;
import com.geo.geotool.service.trans.GeometryFormatTrans;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * • @Description: TODO
 * • @author  : yell
 * • @date : 2021/11/18  14:36
 */
@Service
@Slf4j
public class FeatureServiceImpl implements IFeatureService {
    FeatureMapper featureMapper;
    @Autowired
    public void setFeatureMapper(FeatureMapper featureMapper){
        this.featureMapper = featureMapper;
    }
    @Override
    public void saveFeature(SFeatureEntity sFeatureEntity) throws Exception{
        String the_geom = sFeatureEntity.getGeom();
        log.info("原始GeoJSON:{}",the_geom);
        GeometryFormatTrans geometryFormatTrans = new GeometryFormatTrans();
        sFeatureEntity.setGeom(geometryFormatTrans.geoFormatTrans(the_geom));
        log.info("转换GeoJSON:{}",sFeatureEntity);
        featureMapper.saveFeature(sFeatureEntity.buildFeatureSQL());
    }

    @Override
    public void delFeature(DFeatureEntity dFeatureEntity) {

    }

    @Override
    public void updateFeature(UFeatureEntity uFeatureEntity) {

    }

    @Override
    public List<Map> queryFeature(QFeatureEntity qFeatureEntity) throws Exception{

        String sql = qFeatureEntity.buildFeatureSQL();
        log.info("sql:{}",sql);
        List<Map> list = featureMapper.queryFeature(sql);
        return list;
    }

    /****
     * 对地理坐标串进行预处理
     * @return
     */
    private String handTheGeom(String theGeom) throws Exception{
        String transGeom = formatTrans(theGeom);
        return null;
    }

    /**
     * 坐标转换
     * @return
     */
    private String formatTrans(String theGeom) throws Exception{
        String theFormat = GeometryFormatCapture.capture(theGeom);
        log.info("坐标串:{},格式:{}",theGeom,theFormat);
        GeometryFormatTrans geometryFormatTrans = new GeometryFormatTrans();
        String transGeom  = geometryFormatTrans.geoFormatTrans(theGeom,theFormat,GeometryFormatTrans.defaultTargetGeoFormat);
        return transGeom;
    }

    /**
     * 坐标系转换
     * @return
     */
    private String coordinateSystemTrans(String theGeom,String sourceSrId,String targetSrId){
        return null;
    }
}
